<?php
namespace app\sysadmin\controller;
use think\Db;
use Env;
use Wy\Content as WyContent;
class Contentcat extends Common{
	private $content_type = array();
	private $content_table = array();
	private $wy_content_api = '';
	public function __construct(){
		parent::__construct();

		$this->wy_content_api = new WyContent;
		$this->content_type = config('app_content.content_type');
		$content_table = cache('sysadmin_content_table');
		if(!$content_table){
			$_where = array();
			$_where[] = array('is_del','=','0');
			$content_table = Db::name('content_model_table')->where($_where)->order('id asc')->field('id,name,category_tpl,lists_tpl,show_tpl')->select();
			if($content_table){
				ArrayKey($content_table);
				foreach($content_table as $k=>$v){
					$_table_name = config('database.prefix').'content_lists_'.$v['id'];
					$content_table[$k]['table_name'] = $_table_name;
				}
			}
			cache('sysadmin_content_table',$content_table);
		}
		$this->content_table = $content_table;
	}
	public function index(){
		$cat_list = $this->wy_content_api->getSonLists();
		$content_type = $this->content_type;
		$content_table = $this->content_table;

		foreach($cat_list as $k=>$v){
			$_type_name = $content_type[$v['type']]['name'];
			$table_num = '';
			if(in_array($v['type'],array('1'))){
				$_type_name .= '&nbsp;[ '.$content_table[$v['table_id']]['name'].' ]';
				$table_num = $this->wy_content_api->get_cat_num($content_table[$v['table_id']]['table_name'],$v['id']);
			}
			$cat_list[$k]['type_name'] = $_type_name;
			$cat_list[$k]['table_num'] = $table_num;
			$cat_list[$k]['url'] = $this->wy_content_api->get_cat_url($v['id']);
		}
		$this->assign('cat_list',$cat_list);
		return $this->fetch();
	}
/*
 '1' => [
		'id' => '1',
		'name' => '文章列表',
		'info' => '',
	],
'2' => [
		'id' => '2',
		'name' => '单页详情',
		'info' => '',
	],
'3' => [
		'id' => '3',
		'name' => '栏目页',
		'info' => '',
	],
*/
	private function update_route(){
		$lists = Db::name('content_cat')
			->where('is_del','=','0')
			->where('status','=','1')
			->where('type','in',[1,2,3])
			->where('route','<>','')
			->field('id,route,type')
			->select();
			
		$_route = [];
		
		$_route[] = ['article/:cid', 'article/index'];
		$_route[] = ['category/:cid', 'category/index'];
		$_route[] = ['content/:cid', 'content/index'];
		$_route[] = ['lists/<cid>_<page>', 'lists/index'];
		$_route[] = ['lists/<cid>', 'lists/index'];
		
		if($lists){
			foreach($lists as $k=>$v){
				if($v['type'] == 1){
					$_route[] = [$v['route'].'_<page>','lists/index?cid='.$v['id']];
					$_route[] = [$v['route'],'lists/index?cid='.$v['id']];
				}elseif($v['type'] == 2){
					$_route[] = [$v['route'],'content/index?cid='.$v['id']];
				}elseif($v['type'] == 3){
					$_route[] = [$v['route'],'category/index?cid='.$v['id']];
				}else{
					
				}
			}
		}
		rsort($_route);
		// print_r($_route);
		// exit;
		
		$route_path   = Env::get('route_path');
		
		
		$_config_index = $route_path.'index.php';
		if(!is_writeable($_config_index)){
			$this->error("路由文件".$_config_index.'没有写入权限，请修改服务器权限');
		}
		
		$_php = "<?php \r\n";
		$_php .= "Route::domain(PC_DOMAIN,function(){ \r\n";
		
		if($_route){
			foreach($_route as $k=>$v){
				$_php .= "    Route::get('".$v[0]."', '".$v[1]."'); \r\n";
			}
		}
		
		$_php .= "}, \r\n";
		$_php .= "    ['ext'=>'html'], \r\n";
		$_php .= "    ['cid'=>'\d+','page'=>'\d+','name'=>'\w+'] \r\n";
		$_php .= "); \r\n";
		$_php .= "return []; \r\n";
		$_php .= "?>";
		
		// echo $_config_name;
		// echo $_php;
		
		// return file_put_contents($_config_index,strip_whitespace($_php));
		return file_put_contents($_config_index,$_php);
	}
	public function add(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');

			$_data_info = $this->create_data_array(0,$_post_data);
			if($_data_info['code'] == 0){
				$this->error($_data_info['msg']);exit;
			}
			$_data = $_data_info['data'];

			$_data['create_time'] = time();

			$_ret_id = Db::name('content_cat')->insertGetId($_data);
			if($_ret_id){
				$this->add_operation_log($_ret_id,$_data);
				$this->wy_content_api->edit_cat_path($_ret_id,$_data['up_id']);
				$this->update_route();
				$this->success("添加成功",url('index'));
			}else{
				$this->error("添加失败");
			}
			exit;
		}

		$content_type = $this->content_type;
		$content_table = $this->content_table;
		$this->assign('content_type',$content_type);
		$this->assign('content_table',$content_table);

		$cat_list = $this->wy_content_api->getSonLists();
		ArrayKey($cat_path);
		foreach($cat_list as $k=>$v){
			$cat_list[$k]['path_arr'] = String2Array($cat_path[$v['id']]['path'],'-');
		}
		$this->assign('cat_list',$cat_list);

		$_info = array(
			'id'=>'0',
			'cat_name'=>'',
			'cate_name'=>'',
			'up_id'=>'0',
			'type'=>'1',
			'table_id'=>'0',
			'sort'=>'0',
			'route'=>'',
			'url'=>'',
			'status'=>'1',

			'category_tpl'=>'',
			'lists_tpl'=>'',
			'show_tpl'=>'',
			'content_tpl'=>'',
		);
		$this->assign('info',$_info);
		$this->assign('page_name','添加栏目');
		return $this->fetch('info');
	}
	public function edit(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');

			$_id = (int)$_post_data['id'];

			$_data_info = $this->create_data_array($_id,$_post_data);
			if($_data_info['code'] == 0){
				$this->error($_data_info['msg']);exit;
			}
			$_data = $_data_info['data'];

			$_ret_id = Db::name('content_cat')->where('id','=',$_id)->update($_data);
			if($_ret_id){
				$this->add_operation_log($_id,$_data);
				$this->wy_content_api->edit_cat_path($_id,$_data['up_id']);
				$this->update_route();
				$this->success("编辑成功",url('index'));
			}else{
				$this->error("编辑失败");
			}
			exit;
		}
		$_id = (int)(input('get.id'));
		$_info = Db::name('content_cat')->where('id','=',$_id)->find();
		if(!$_info){
			return $this->tips_404();exit;
		}
		$this->assign('info',$_info);

		$content_type = $this->content_type;
		$content_table = $this->content_table;
		$this->assign('content_type',$content_type);
		$this->assign('content_table',$content_table);

		$cat_list = $this->wy_content_api->getSonLists();
		if($cat_list){
			$cat_path = Db::name('content_cat_path')->field('id,path')->select();
			ArrayKey($cat_path);
			foreach($cat_list as $k=>$v){
				$cat_list[$k]['path_arr'] = String2Array($cat_path[$v['id']]['path'],'-');
			}
		}

		$this->assign('cat_list',$cat_list);

		$this->assign('page_name','编辑栏目');
		return $this->fetch('info');
	}
	public function del(){
		if($this->request->isGet() == true){
			$_id = (int)input('get.id');
			if(!$_id){
				$this->error("请选择需要删除的信息！");
			}
			$_info = Db::name('content_cat')->where('id','=',$_id)->where('is_del','=','0')->find();
			if(!$_info){
				$this->error("请选择需要删除的信息！");
			}
			$_son_count = Db::name('content_cat')->where('up_id','=',$_id)->where('is_del','=','0')->count();
			if($_son_count > 0){
				$this->error("该栏目下面包含子栏目，请先删除子栏目");
			}
			// content_cat_path
			// content_page
			$_ret = Db::name('content_cat')->where('id','=',$_id)->update(array('is_del'=>1));
			if($_ret){
				$this->add_operation_log($_id,$_info);

				cache('cat_all_data_url',null);
				$this->update_route();

				$this->success("删除成功",url('index'));
			}else{
				$this->error("删除失败，请刷新以后重试！");
			}

		}else{
			$this->error("系统错误！");
		}
	}
	public function content(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');

			$_id = (int)$_post_data['id'];

			$_data = array();
			$_data['title'] = $_post_data['title'];
			$_data['banner'] = $_post_data['banner'];
			$_data['seo_title'] = $_post_data['seo_title'];
			$_data['seo_keywords'] = $_post_data['seo_keywords'];
			$_data['seo_description'] = $_post_data['seo_description'];
			$_data['update_time'] = time();

			$_ret_id = Db::name('content_page')->where('id','=',$_id)->update($_data);
			if($_ret_id){
				$this->add_operation_log($_id,$_data);
				$this->success("编辑成功",url('index'));
			}else{
				$this->error("编辑失败");
			}
			exit;
		}
		$_id = (int)(input('get.id'));
		$_info = Db::name('content_page')->where('id','=',$_id)->find();
		if(!$_info){
			return $this->tips_404();exit;
		}
		$this->assign('info',$_info);

		$this->assign('page_name','编辑内容');
		return $this->fetch('content');
	}

	private function create_data_array($_id=0,$_post_data = array()){
		$_ret = array('code'=>0,'msg'=>'error','data'=>array());
		$_data = array();
		$_data['cat_name']	= $_post_data['cat_name'];
		$_data['cate_name']	= $_post_data['cate_name'];
		$_data['up_id']		= (int)$_post_data['up_id'];
		$_data['status']	= (int)$_post_data['status'] == 1 ? 1 : 0;
		$_data['sort']		= (int)$_post_data['sort'];
		$_data['type']		= in_array($_post_data['type'],array(1,2,3,4,0))?$_post_data['type']:0;


		$_table_id		= $_post_data['table_id'];
		$_route			= $_post_data['route'];
		$_category_tpl	= $_post_data['category_tpl'];
		$_lists_tpl		= $_post_data['lists_tpl'];
		$_show_tpl		= $_post_data['show_tpl'];
		$_content_tpl	= $_post_data['content_tpl'];
		$_url			= $_post_data['url'];

		if($_data['type'] == 1){
			$_category_tpl	= '';
			$_content_tpl	= '';
			$_url			= '';
		}elseif($_data['type'] == 2){
			$_table_id		= '0';
			$_category_tpl	= '';
			$_lists_tpl		= '';
			$_show_tpl		= '';
			$_url			= '';
		}elseif($_data['type'] == 3){
			$_table_id		= '0';
			$_lists_tpl		= '';
			$_show_tpl		= '';
			$_content_tpl	= '';
			$_url			= '';
		}elseif($_data['type'] == 4){

			$_table_id		= '0';
			$_route			= '';
			$_category_tpl	= '';
			$_lists_tpl		= '';
			$_show_tpl		= '';
			$_content_tpl	= '';
			$_url			= '';
		}elseif($_data['type'] == 0){

			$_table_id		= '0';
			$_route			= '';
			$_category_tpl	= '';
			$_lists_tpl		= '';
			$_show_tpl		= '';
			$_content_tpl	= '';

		}else{

			$_ret['msg'] = '系统错误，请刷新后在操作';
			return $_ret;

		}
		if($_route){
			$_where_route = array();
			$_where_route[] = array('route','like',$_route);
			if($_id > 0){
				$_where_route[] = array('id','<>',$_id);
			}
			$_route_count = Db::name('content_cat')->where($_where_route)->where('is_del','=','0')->count();
			if($_route_count > 0){
				$_ret['msg'] = '伪静态标识重复';
				return $_ret;
			}
		}

		$_data['table_id']		= $_table_id;
		$_data['route']			= $_route;
		$_data['url']			= $_url;
		$_data['category_tpl']	= $_category_tpl;
		$_data['lists_tpl']		= $_lists_tpl;
		$_data['show_tpl']		= $_show_tpl;
		$_data['content_tpl']	= $_content_tpl;
		$_data['update_time']	= time();

		$_ret['code']	= 1;
		$_ret['msg']	= 'success';
		$_ret['data']	= $_data;
		return $_ret;
	}
}
?>